﻿using Prism.Commands;
using Prism.Events;
using Prism.Regions;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using WPF.Core;
using WPF.Zero.Services;

namespace WPF.Zero.ViewModels
{
    internal class MainViewModel : NavigateViewModel
    {
        private readonly INavigationMenuService menuService;
        private readonly IRegionManager region;
        private IRegion NavigationRegion => region.Regions["MainRegion"];
        private ObservableCollection<NavigationItem> navigationItems;

        public ObservableCollection<NavigationItem> NavigationItems
        {
            get { return navigationItems; }
            set { navigationItems = value; OnPropertyChanged(); }
        }

        public DelegateCommand<NavigationItem> NavigateCommand { get; private set; }

        public MainViewModel(INavigationMenuService menuService,
              IRegionManager region,
              IEventAggregator eventAggregator)
        {
            this.menuService = menuService;
            this.region = region;
            NavigationItems = menuService.GetNavigationItems();
            NavigateCommand = new DelegateCommand<NavigationItem>(Navigate);

            eventAggregator.GetEvent<RefreshLanguageEvent>().Subscribe(RefreshLanguage);
        }

        private int selectedIndex;

        public int SelectedIndex
        {
            get { return selectedIndex; }
            set { selectedIndex = value; OnPropertyChanged(); }
        }

        private void RefreshLanguage(bool refresh)
        {
            foreach (var item in NavigationItems)
            {
                item.Title = AppSession.Instance[item.Key];
                if (item.Items != null && item.Items.Count > 0)
                {
                    foreach (var sub in item.Items)
                        sub.Title = AppSession.Instance[sub.Key];
                }
            }
        }

        private void Navigate(NavigationItem item)
        {
            if (item == null) return;

            region.Regions["MainRegion"].RequestNavigate(item.PageName, callback =>
            {
                if ((bool)callback.Result)
                { }
            });

            var view = NavigationRegion.Views.FirstOrDefault(q => q.GetType().Name.Equals(item.PageName));
            if (view == null)
            {
                NavigationRegion.RequestNavigate(item.PageName, NavigateionCallBack, null);
            }
            else
            {
                SelectedIndex = NavigationRegion.Views.IndexOf(view);
            }
        }

        public override void OnNavigatedTo(NavigationContext navigationContext)
        {
            RefreshLanguage(true);
            region.Regions["MainRegion"].RequestNavigate("DashboardView");
            base.OnNavigatedTo(navigationContext);
        }

        public void RemoveView(object view)
        {
            if (NavigationRegion.Views.Contains(view))
            {
                /*
                 * 关闭Tab后调用OnNavigatedFrom，如需手动释放资源请在 OnNavigatedFrom 中处理
                 */
                if (view is UserControl viewControl && viewControl.DataContext is INavigationAware navigationAware)
                    navigationAware.OnNavigatedFrom(null);

                NavigationRegion.Remove(view);
            }
        }

        private void NavigateionCallBack(NavigationResult navigationResult)
        {
            if (navigationResult.Result != null && !(bool)navigationResult.Result)
            {
#if DEBUG
                System.Diagnostics.Debug.WriteLine(navigationResult.Error.Message);
#endif
            }
            else
            {
                SelectedIndex = NavigationRegion.Views.Count() - 1;
            }
        }
    }
}
